3e77bb
@@ -65,6 +65,8 @@
 import org.apache.hive.jdbc.Utils;
 import org.apache.hive.jdbc.Utils.JdbcConnectionParams;
 
+import com.google.common.annotations.VisibleForTesting;
+
 
 public class Commands {
   private final BeeLine beeLine;
@@ -1042,11 +1044,39 @@
private boolean executeInternal(String sql, boolean call) {
     return true;
   }
 
+  //startQuote use array type in order to pass int type as input/output parameter.
+  //This method remove comment from current line of a query.
+  //It does not remove comment like strings inside quotes.
+  @VisibleForTesting
+  String removeComments(String line, int[] startQuote) {
+    if (line == null || line.isEmpty()) return line;
+    if (startQuote[0] == -1 && beeLine.isComment(line)) return "";  //assume # can only be used at the beginning of line.
+    StringBuilder builder = new StringBuilder();
+    for (int index = 0; index < line.length(); index++) {
+      if (startQuote[0] == -1 && index < line.length() - 1 && line.charAt(index) == '-' && line.charAt(index + 1) =='-') {
+        return builder.toString().trim();
+      }
+
+      char letter = line.charAt(index);
+      if (startQuote[0] == letter && (index == 0 || line.charAt(index -1) != '\\') ) {
+        startQuote[0] = -1; // Turn escape off.
+      } else if (startQuote[0] == -1 && (letter == '\'' || letter == '"') && (index == 0 || line.charAt(index -1) != '\\')) {
+        startQuote[0] = letter; // Turn escape on.
+      }
+
+      builder.append(letter);
+    }
+
+    return builder.toString().trim();
+  }
+
   /*
    * Check if the input line is a multi-line command which needs to read further
    */
   public String handleMultiLineCmd(String line) throws IOException {
     //When using -e, console reader is not initialized and command is always a single line
+    int[] startQuote = {-1};
+    line = removeComments(line,startQuote);
     while (isMultiLine(line) && beeLine.getOpts().isAllowMultiLineCommand()) {
       StringBuilder prompt = new StringBuilder(beeLine.getPrompt());
       if (!beeLine.getOpts().isSilent()) {
@@ -1071,7 +1101,8 @@
public String handleMultiLineCmd(String line) throws IOException {
       if (extra == null) { //it happens when using -f and the line of cmds does not end with ;
         break;
       }
-      if (!beeLine.isComment(extra)) {
+      extra = removeComments(extra,startQuote);
+      if (extra != null && !extra.isEmpty()) {
         line += "\n" + extra;
       }
     }
